﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace Projektarbete
{
    class NetPlayer
    {
        public enum MovementDirection { none, up, down, left, right };

        private string name;
        private ushort health;
        private float movementSpeed;
        public Vector2 position;
        private Texture2D spritesheet;

        public ushort getHealth { get { return health; } }
        public string getName { get { return name; } }
        public bool IsPlaying { get; set; }

        private bool isMoving;

        private MovementDirection movementDirection;
        private MovementDirection facingDirection;

        //keyLayout är i den här ordningen:
        /*
         * [0] = Gå uppåt
         * [1] = Gå neråt
         * [2] = Gå vänster
         * [3] = Gå höger
         * [4] = Interact
         * [5] = Modifyer key
        */

        /// <summary>
        /// Create a new player
        /// </summary>
        /// <param name="name">Name of the player</param>
        /// <param name="health">Health of the player</param>
        /// <param name="movementSpeed">Movement speed</param>
        /// <param name="position">Intitial position</param>
        /// <param name="spritesheet">Spritesheet to use</param>
        /// <param name="keyLayout">[0] = walk up, [1] = walk down, [2] = walk left, [3] = walk right, </param>
        public NetPlayer(string name, ushort health, float movementSpeed, Vector2 position, Texture2D spritesheet)
        {
            this.name = name;
            this.health = health;
            this.spritesheet = spritesheet;
            this.position = position;
            this.movementSpeed = movementSpeed;

            isMoving = false;

            movementDirection = MovementDirection.none;
            facingDirection = MovementDirection.none;
        }

        /// <summary>
        /// Moves the player in the direction defined in this
        /// </summary>
        private void Move()
        {
            switch (movementDirection)
            {
                case MovementDirection.up:
                    position.Y -= movementSpeed;
                    if (position.Y % 24 == 0)
                    {
                        movementDirection = MovementDirection.none;
                        isMoving = false;
                    }
                    break;
                case MovementDirection.down:
                    position.Y += movementSpeed;
                    if (position.Y % 24 == 0)
                    {
                        movementDirection = MovementDirection.none;
                        isMoving = false;
                    }
                    break;
                case MovementDirection.left:
                    position.X -= movementSpeed;
                    if (position.X % 24 == 0)
                    {
                        movementDirection = MovementDirection.none;
                        isMoving = false;
                    }
                    break;
                case MovementDirection.right:
                    position.X += movementSpeed;
                    if (position.X % 24 == 0)
                    {
                        movementDirection = MovementDirection.none;
                        isMoving = false;
                    }
                    break;
                default:
                    break;
            }
        }

        /// <summary>
        /// Get the tiled position of the player
        /// </summary>
        /// <returns>A vector2 containing the tiled position</returns>
        public Vector2 GetTiledPositionToVector2()
        {
            return new Vector2((float)Math.Floor(position.X / 24), (float)Math.Floor(position.Y / 24));
        }

        /// <summary>
        /// Get the tiled position of the player
        /// </summary>
        /// <returns>A Point containing the tiled position</returns>
        public Point GetTiledPositionToPoint()
        {
            return new Point((int)Math.Floor(position.X / 24), (int)Math.Floor(position.Y / 24));
        }

        /// <summary>
        /// Draw the character
        /// </summary>
        /// <param name="spriteBatch">The sprite batch</param> 
        public void DrawCharacter(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(spritesheet, position, Color.White);
        }
    }
}